package com.personal.gateway.flinkAllCommand.configurationOption.configurationType;

import com.personal.gateway.flinkAllCommand.configurationOption.ConfigurationOption;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;

import java.util.Set;

/**
 * @author BigHead
 * @date 2022/1/10 16:33
 * @description ConfigurationTypeClass
 */
@Slf4j
public class ConfigurationTypeClass extends AbstractConfigurationType<AbstractConfigurationTypeClass> {

    private static final String PROJECT_BASE_PATH = "com";

    public ConfigurationTypeClass(Class<AbstractConfigurationTypeClass> configurationType) {
        super(configurationType);
    }

    @Override
    public String getTypeString() {
        return configurationType.getSimpleName();
    }

    @Override
    public void setValue(AbstractConfigurationTypeClass value) {}

    @Override
    public void setExampleValue(AbstractConfigurationTypeClass... exampleValue) {

    }

    @Override
    public AbstractConfigurationTypeClass[] getExampleValue(ConfigurationOption<?, ?> configurationOption) {
        return new AbstractConfigurationTypeClass[0];
    }

    @Override
    public AbstractConfigurationTypeClass getValue(){
        try {
            return configurationType.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            log.error("", e);
            e.printStackTrace(); // TODO 这里理论上不会出现异常 但是出现异常捕获方式不正确
        }
        // TODO 逻辑上永远不会出现
        return null;
    }

    public AbstractConfigurationTypeClass getValue(ConfigurationOption<?, ?> configurationOption){
        Reflections reflections = new Reflections("com");
        Set<Class<? extends AbstractConfigurationTypeClass>> classSet = reflections.getSubTypesOf(configurationType);
        for (Class<? extends AbstractConfigurationTypeClass> clazz: classSet){
            AbstractConfigurationTypeClass abstractConfigurationTypeClass = null;
            try {
                abstractConfigurationTypeClass = clazz.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                log.error("", e);
                e.printStackTrace();
            }
            if (abstractConfigurationTypeClass.checkAntecedent(configurationOption)){
                return abstractConfigurationTypeClass;
            }
        }
        return null;
    }
}
